package com.tianda.his.modules.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianda.his.commons.utils.LoginUtils;
import com.tianda.his.commons.vo.AjaxResult;
import com.tianda.his.modules.system.dto.DeptDto;
import com.tianda.his.modules.system.dto.DictDataDto;
import com.tianda.his.modules.system.entity.SysDept;
import com.tianda.his.modules.system.entity.SysDictData;
import com.tianda.his.modules.system.service.SysDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 部门管理
 */
@RestController
//@RequestMapping("/system/dept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 分页查询
     * @param deptDto
     * @return
     */
    @GetMapping("/system/dept/listDeptForPage")
    public AjaxResult listDeptForPage(DeptDto deptDto){
        Page<SysDept> pageResult = sysDeptService.listDeptForPage(deptDto);
        return AjaxResult.success(pageResult.getRecords(), pageResult.getTotal());
    }

    /**
     * 新增字典数据
     * @return
     */
    @PostMapping("/system/dept/addDept")
    public AjaxResult addDept(SysDept sysDept){

        sysDept.setUpdateBy(LoginUtils.getLoginUser().getUserName());
        sysDept.setCreateTime(new Date());

        //新增部门
        sysDeptService.save(sysDept);

        return AjaxResult.success();
    }

    /**
     * 根据部门id查询部门
     * @param deptId
     * @return
     */
    @GetMapping("system/dept/getDeptById/{deptId}")
    public AjaxResult getDeptById(@PathVariable("deptId") Long deptId){
        return AjaxResult.success(sysDeptService.getById(deptId));
    }

    /**
     * 修改部门
     * @param sysDept
     * @return
     */
    @PutMapping("/system/dept/updateDept")
    public AjaxResult updateDept(SysDept sysDept){
        sysDept.setUpdateBy(LoginUtils.getLoginUser().getUserName());
        sysDept.setUpdateTime(new Date());
        sysDeptService.updateById(sysDept);
        return AjaxResult.success();
    }

    /**
     * 根据id删除部门
     * @param deptId
     * @return
     */
    @DeleteMapping("/system/dept/deleteDeptByIds/{deptId}")
    public AjaxResult deleteDeptById(@PathVariable("deptId") Long deptId){
        sysDeptService.removeById(deptId);
        return AjaxResult.success();
    }

    /**
     * 查询所有部门
     * @return
     */
    @GetMapping("/system/dept/selectAllDept")
    public AjaxResult selectAllDept(){
        return AjaxResult.success(sysDeptService.list());
    }

}
